// pages/product/index.js
const App = getApp();
const AJAX = require("../../../../common/request")
const API = require("../../../../common/api")
const common = require("../../../../common/common")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navTop: 0,
        navContentHeight: 0,
        placeholderHeight: 0,
        drugClass: [],
        drugData: [],
        UPLOADURL: "",
        isTabs: false,
        total:0,
        page:1,
        params: {
            drugIntroductionName: "",
            drugClassificationLabel: [],
            drugClassificationDrugclass: [],
            drugClassificationDosage: [],
            drugClassificationCrowd: [],
            drugClassificationUsage: [],
            drugClassificationIndications: [],
        },
    },
    drugIntroductionNameInput: function (e) {
        this.setData({
            [`params.drugIntroductionName`]: e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        this.setData({
            navTop: App.globalData.navTop,
            navContentHeight: App.globalData.navContentHeight,
            options: JSON.stringify(options),
            UPLOADURL: API.UPLOADURL,
            [`params.drugIntroductionName`]: options.seach_text ? options.seach_text : '',
        })
        const query = wx.createSelectorQuery();
        query.select('.head').boundingClientRect();
        await query.exec((res) => {
            console.log(res)
            this.setData({
                placeholderHeight: res[0].height,
            })
        });
        this.getbDrugClasslist()
        this.search()
    },
    async getbDrugClasslist(){
        await AJAX.POST('/system/AppWechat/getbDrugClasslist', {}).then((res) => {
            if (res.code == 200) {
                this.setData({
                    drugClass: res.rows
                })
            }
        })
    },
    Tabs(e) {
        console.log(e)
        this.setData({
            isTabs: this.data.isTabs == false ? true : false
        })
    },
    async onPullDownRefresh(){
        this.setData({
            page:1,
            total:0,
            drugClass: [],
            drugData:[],
            params: {
                drugIntroductionName: "",
                drugClassificationLabel: [],
                drugClassificationDrugclass: [],
                drugClassificationDosage: [],
                drugClassificationCrowd: [],
                drugClassificationUsage: [],
                drugClassificationIndications: [],
            },
        })
        await this.getbDrugClasslist()
        this.search()
        wx.stopPullDownRefresh()
    },
    setdrugClass(e) {
        console.log(e.currentTarget.dataset)
        //取点击的分类名称
        let drugName = this.data.drugClass[e.currentTarget.dataset.classid].drugName
        let drugId = this.data.drugClass[e.currentTarget.dataset.classid].childen[e.currentTarget.dataset.clidenid].id
        let status = this.data.drugClass[e.currentTarget.dataset.classid].childen[e.currentTarget.dataset.clidenid].status ? this.data.drugClass[e.currentTarget.dataset.classid].childen[e.currentTarget.dataset.clidenid].status : false
        console.log(drugName)
        console.log(drugId)
        console.log(status)
        this.setData({
            [`drugClass[${e.currentTarget.dataset.classid}].childen[${e.currentTarget.dataset.clidenid}].status`]: status == true ? false : true
        })

        if (drugName == '标签分类') {
            let drugClassificationLabel = this.data.params.drugClassificationLabel
            let index = this.data.params.drugClassificationLabel.indexOf(drugId)
            if (status == true) {
                drugClassificationLabel.splice(index, 1)
            } else {
                drugClassificationLabel.push(drugId)
            }
            this.setData({
                [`params.drugClassificationLabel`]: drugClassificationLabel
            })
        }
        if (drugName == '药品分类') {
            let drugClassificationDrugclass = this.data.params.drugClassificationDrugclass
            let index = this.data.params.drugClassificationDrugclass.indexOf(drugId)
            if (status == true) {
                drugClassificationDrugclass.splice(index, 1)
            } else {
                drugClassificationDrugclass.push(drugId)
            }
            this.setData({
                [`params.drugClassificationDrugclass`]: drugClassificationDrugclass
            })
        }
        if (drugName == '药品剂型') {
            let drugClassificationDosage = this.data.params.drugClassificationDosage
            let index = this.data.params.drugClassificationDosage.indexOf(drugId)
            if (status == true) {
                drugClassificationDosage.splice(index, 1)
            } else {
                drugClassificationDosage.push(drugId)
            }
            this.setData({
                [`params.drugClassificationDosage`]: drugClassificationDosage
            })
        }
        if (drugName == '适用人群') {
            let drugClassificationCrowd = this.data.params.drugClassificationCrowd
            let index = this.data.params.drugClassificationCrowd.indexOf(drugId)
            if (status == true) {
                drugClassificationCrowd.splice(index, 1)
            } else {
                drugClassificationCrowd.push(drugId)
            }
            this.setData({
                [`params.drugClassificationCrowd`]: drugClassificationCrowd
            })
        }
        if (drugName == '使用方式') {
            let drugClassificationUsage = this.data.params.drugClassificationUsage
            let index = this.data.params.drugClassificationUsage.indexOf(drugId)
            if (status == true) {
                drugClassificationUsage.splice(index, 1)
            } else {
                drugClassificationUsage.push(drugId)
            }
            this.setData({
                [`params.drugClassificationUsage`]: drugClassificationUsage
            })
        }
        if (drugName == '适用症状') {
            let drugClassificationIndications = this.data.params.drugClassificationIndications
            let index = this.data.params.drugClassificationIndications.indexOf(drugId)
            if (status == true) {
                drugClassificationIndications.splice(index, 1)
            } else {
                drugClassificationIndications.push(drugId)
            }
            this.setData({
                [`params.drugClassificationIndications`]: drugClassificationIndications
            })
        }
    },
    
    search() {
        this.setData({
            page: 1,
            total:0,
            drugData: [],
        })
        this.getbDrugIntroductionlist()  
    },
    //获取数据
    getbDrugIntroductionlist(){
        let params = {}
        params.drugIntroductionName = this.data.params.drugIntroductionName
        params.drugClassificationLabel = this.data.params.drugClassificationLabel.join(',')
        params.drugClassificationDrugclass = this.data.params.drugClassificationDrugclass.join(',')
        params.drugClassificationDosage = this.data.params.drugClassificationDosage.join(',')
        params.drugClassificationCrowd = this.data.params.drugClassificationCrowd.join(',')
        params.drugClassificationUsage = this.data.params.drugClassificationUsage.join(',')
        params.drugClassificationIndications = this.data.params.drugClassificationIndications.join(',')
   
        let page = this.data.page
        if(this.data.total > 0 &&  (page-1) * 10  > this.data.total){
            console.log("没数据")
            return false;
        }
        if(page == 1){
            wx.pageScrollTo({
                scrollTop: 0,
            });
        }
        let oldData = this.data.drugData
        AJAX.POST("/system/AppWechat/getbDrugIntroductionlist?pageSize=10&pageNum="+page, params).then((res) => {
            if (res.code == 200) {
                oldData = oldData.concat(res.rows)
                this.setData({
                    drugData: oldData,
                    total:res.total,
                    page:page+1,
                })
            }
            this.setData({
                isTabs: false
            })
        })

    },

    onReachBottom(){
        if(this.data.isTabs == true){
            return
        }
        this.getbDrugIntroductionlist()
    },
 
    //药品详情
    goDrugDetail(e) {
        console.log(e.currentTarget.dataset)
        wx.navigateTo({
            url: '/pages/index/drug/detail/index?id=' + this.data.drugData[e.currentTarget.dataset.index].id,
        })
    },
    goBack() {
        const pages = getCurrentPages();
        //获取不到上级返回首页
        if (pages.length > 1) {
            wx.navigateBack({})
        } else {
            wx.reLaunch({
                url: '/pages/index/index',
            })
        }
    },
    onShareAppMessage(options) {
        // 默认分享配置
        return {
            title: App.globalData.onShareAppMessage.title,
            path: App.globalData.onShareAppMessage.path,
            imageUrl: App.globalData.onShareAppMessage.imageUrl,
        }
    },
})